package com.iotproject.studyservice.controller;

import com.iotproject.commonutils.R;
import com.iotproject.studyservice.entity.StudyGrade;
import com.iotproject.studyservice.entity.StudyWord;
import com.iotproject.studyservice.entity.UserRememberWord;
import com.iotproject.studyservice.entity.UserStudyWord;
import com.iotproject.studyservice.service.StudyGradeService;
import com.iotproject.studyservice.service.StudyWordService;
import com.iotproject.studyservice.service.UserRememberWordService;
import com.iotproject.studyservice.service.UserStudyWordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/studyservice/remember-word")
public class RememberWordController {

    @Autowired
    StudyWordService studyWordService;

    @Autowired
    UserRememberWordService userRememberWordService;

    @Autowired
    UserStudyWordService userStudyWordService;

    @Autowired
    StudyGradeService studyGradeService;

    //点击记住单词
    @GetMapping("rememberWord/{wordId}")
    public R rememberWord(@PathVariable("wordId") String wordId, HttpServletRequest httpServletRequest) {
        //展示单词意思
        StudyWord studyWord = studyWordService.getById(wordId);
        //查询等级名称
        String grade = studyWord.getGrade();
        StudyGrade studyGrade = studyGradeService.getById(grade);
        String gradeName = studyGrade.getName();
        //添加新记录
        String memberUserId = studyWordService.getMemberUserId(httpServletRequest);
        //向已记住表添加数据
        //先查询数据是否存在
        QueryWrapper<UserRememberWord> userRememberWordQueryWrapper = new QueryWrapper<>();
        userRememberWordQueryWrapper.eq("user_id", memberUserId);
        userRememberWordQueryWrapper.eq("remember_word_id", wordId);
        int rememberWordRecord = userRememberWordService.count(userRememberWordQueryWrapper);
        if (rememberWordRecord == 0) {//新数据
            UserRememberWord userRememberWord = new UserRememberWord();
            userRememberWord.setUserId(memberUserId);
            userRememberWord.setRememberWordId(wordId);
            userRememberWord.setGrade(grade);
            userRememberWordService.save(userRememberWord);
        }
        //向已学习表添加数据
        QueryWrapper<UserStudyWord> userStudyWordQueryWrapper = new QueryWrapper<>();
        userStudyWordQueryWrapper.eq("user_id", memberUserId);
        userStudyWordQueryWrapper.eq("study_word_id", wordId);
        int studyWordRecord = userStudyWordService.count(userStudyWordQueryWrapper);
        if (studyWordRecord == 0) {
            UserStudyWord userStudyWord = new UserStudyWord();
            userStudyWord.setUserId(memberUserId);
            userStudyWord.setStudyWordId(wordId);
            userStudyWord.setGrade(grade);
            userStudyWordService.save(userStudyWord);
        }
        //展示进度条信息
        Map<String, Double> progress = studyWordService.progress(grade, httpServletRequest);
        Double rememberPercent = progress.get("rememberPercent");
        Double forgetPercent = progress.get("forgetPercent");

        return R.ok().data("gradeName", gradeName).data("word", studyWord).data("rememberPercent", rememberPercent).data("forgetPercent", forgetPercent);
    }



}
